/**
* Created by wust on 2019/5/14.
*/
<template>
  <el-form ref="addModel" :model="addModel" :rules="rules" label-width="120px" @submit.native.prevent>
    <el-form-item label="项目名" prop="name">
      <el-input v-model="addModel.name"></el-input>
    </el-form-item>
    <el-form-item label="管理员账号" prop="adminAccount">
      <el-input v-model="addModel.adminAccount"></el-input>
    </el-form-item>
    <el-form-item label="项目地址">
      <el-input v-model="addModel.addr"></el-input>
    </el-form-item>
    <el-form-item label="备注">
      <el-input
        v-model="addModel.description"
        type="textarea"
        :rows="6">
      </el-input>
    </el-form-item>
    <el-form-item style="text-align: left;">
      <el-button type="primary" @click="onSubmit('addModel')" :loading="submitting">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import Vue from 'vue'

export default {
  name: 'project-create',
  props: ['selectedNode'],
  data () {
    return {
      submitting: false,
      addModel: {
        name: '',
        addr: '',
        description: '',
        adminAccount: ''
      },
      rules: {
        name: [{required: true, message: '请输入项目名称', trigger: 'blur'}],
        adminAccount: [{required: true, message: '请输入管理员账号', trigger: 'blur'}]
      }
    }
  },
  created: function () {
    this.getDefaultAdminAccount()
  },
  methods: {
    getDefaultAdminAccount: function () {
      let userType = 'A100410'
      let accountPrefix = 'XM'

      Vue.$ajax({
        method: 'post',
        url: Vue.$adminServerURL + '/web/v1/OrganizationController/getDefaultAdminAccount?userType=' + userType + '&accountPrefix=' + accountPrefix
      }).then(res => {
        if (res.data.flag !== 'SUCCESS') {
          if (!Vue.$isNullOrIsBlankOrIsUndefined(res.data.message)) {
            this.$message({
              message: res.data.message,
              type: 'warning'
            })
          }
        } else {
          this.addModel.adminAccount = res.data.obj
        }
      })
    },
    onSubmit: function (formData) {
      this.$refs[formData].validate((valid) => {
        if (!valid) {
        } else {
          this.submitting = true
          let organization = {pid: this.selectedNode.id, type: 'A101109'}
          let project = this.addModel
          Vue.$ajax({
            method: 'post',
            url: Vue.$adminServerURL + '/web/v1/OrganizationController/createProject',
            data: {organization: organization, project: project}
          }).then(res => {
            this.submitting = false
            if (res.data.flag !== 'SUCCESS') {
              if (!Vue.$isNullOrIsBlankOrIsUndefined(res.data.message)) {
                this.$message({
                  message: res.data.message,
                  type: 'warning'
                })
              }
            } else {
              this.$message({
                message: res.data.message,
                type: 'success'
              })

              let name = this.addModel.name
              let newNode = {id: res.data.obj.id, pId: this.selectedNode.id, name: name, type: this.addModel.type, relationId: res.data.obj.relationId}
              this.addNode(newNode)
            }
          })
        }
      })
    },
    addNode: function (newNode) {
      this.$emit('addNode', newNode)
    }
  }
}
</script>
